﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ManageBanking.Models;

namespace ManageBanking.Areas.Admin.Controllers
{
    public class ManageUserAdminController : Controller
    {
        //
        // GET: /Admin/ManageUser/
        ManageUserModels mu = new ManageUserModels();
        ManageBanking.Areas.Admin.Models.ManageUserModels admu = new ManageBanking.Areas.Admin.Models.ManageUserModels();
        public ActionResult AddAcc()
        {
            if (Session["Admin"] != null)
            {
                ViewData["menuadmin"] = "AddUser";
                ViewData["Result"] = Session["Result"];
                Session.Remove("Result");
                return View();
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");
        }
        public ActionResult Delete(int id)
        {
            try
            {
                if(admu.DeleteAccount(id)==true)
                Session["Check"] = "true";
                else
                Session["Check"] = "Username Is Used ,Can't Delete";
           
            }
            catch
            {
                Session["Check"] = "Username Is Used ,Can't Delete";
            }
            return RedirectToAction("Paging", "ManageAdmin");
        }
        public ActionResult DeleteLockUser(int id)
        {
            try
            {
                if (admu.DeleteAccount(id) == true)
                    Session["Check"] = "true";
                else
                    Session["Check"] = "Username Is Used ,Can't Delete";

            }
            catch
            {
                Session["Check"] = "Username Is Used ,Can't Delete";
            }
            return RedirectToAction("LockUser", "ManageAdmin");
        }
        public ActionResult DeleteUnLockUser(int id)
        {
            try
            {
                if (admu.DeleteAccount(id) == true)
                    Session["Check"] = "true";
                else
                    Session["Check"] = "Username Is Used ,Can't Delete";

            }
            catch
            {
                Session["Check"] = "Username Is Used ,Can't Delete";
            }
            return RedirectToAction("UnlockUser", "ManageAdmin");
        }
        public ActionResult ResolveAddAcc(int TypeUser, string username, string password, string name, string identificard, string sex, DateTime birth, string address, string Email, string PlaceBirth, string job, string phase, int idadmin)
        {


            if (mu.CheckUSer(username) == true)
            {
                Session["Result"] = "Username is already used";
                return RedirectToAction("AddAcc");
            }
            else if (mu.AnswerIdentity(identificard) == true)
            {
                Session["Result"] = "Identification card is already exist";
                return RedirectToAction("AddAcc");

            }
            else if (mu.Answer_Email(Email) == true)
            {
                Session["Result"] = "Email is already exist";
                return RedirectToAction("AddAcc");

            }


            else
            {
                admu.AddAcc(TypeUser, username, password, name, identificard, sex, birth, address, Email, PlaceBirth, job, phase, idadmin);
                Session["Result"] = "true";

                return RedirectToAction("AddAcc");
            }

        }
        public ActionResult InformationUsername(string id)
        {
            if (Session["Admin"] != null && id != null)
            {
                ManageBanking.Areas.Admin.Models.Username ab = admu.InformationAcc(Convert.ToInt32(id));
                ViewData["Info"] = ab;
                return View();
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");
        }
        public ActionResult UpdateLockUser(string idusername, string lockuser, string page)
        {
            if (Session["Admin"] != null && idusername != null && lockuser != null)
            {
                Username us = (Username)Session["Admin"];
                if (admu.Update(us.IdUsername ,Convert.ToInt32(idusername), lockuser) == true)
                    Session["Check"] = "true";
                else
                    Session["Check"] = "Error, Can't Update Try Again";
                if (page.Equals("pagelock"))
                    return RedirectToAction("LockUser", "ManageAdmin");
                else if (page.Equals("pageunlock"))
                    return RedirectToAction("UnlockUser", "ManageAdmin");
                else
                    return RedirectToAction("LockUser", "ManageAdmin");
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");
        }
        public ActionResult UpdateUnlockUser(string idusername, string lockuser, string page)
        {
            if (Session["Admin"] != null && idusername != null && lockuser != null)
            {
                ManageBanking.Areas.Admin.Models.Username us = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                if (admu.Update(us.IdUsername,Convert.ToInt32(idusername), lockuser) == true)
                    Session["Check"] = "true";
                else
                    Session["Check"] = "Error, Can't Update Try Again";
                if (page.Equals("pagelock"))
                    return RedirectToAction("LockUser", "ManageAdmin");
                else if (page.Equals("pageunlock"))
                    return RedirectToAction("UnlockUser", "ManageAdmin");
                else
                    return RedirectToAction("UnlockUser", "ManageAdmin");
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");
        }

        public ActionResult Update(string idusername, string lockuser, string page)
        {
            if (Session["Admin"] != null && idusername != null && lockuser != null)
            {
                Username us = (Username)Session["Admin"];
                if (admu.Update(us.IdUsername,Convert.ToInt32(idusername), lockuser) == true)
                    Session["Check"] = "true";
                else
                    Session["Check"] = "Error, Can't Update Try Again";
                if (page.Equals("pagelock"))
                    return RedirectToAction("LockUser", "ManageAdmin");
                else if (page.Equals("pageunlock"))
                    return RedirectToAction("UnlockUser", "ManageAdmin");
                else
                    return RedirectToAction("Paging", "ManageAdmin");
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");
        }
        public ActionResult Resolve(int idrequest, string lockuser, int idtype, int idadmin, int idusername)
        {
            if (lockuser == null)
            {
                Session["Res"] = "This Request Is Solved";
                return RedirectToAction("RequestAdmin", "ManageAdmin");
            
            }
            
            if (lockuser.Equals("true"))
            {
                ManageBanking.Areas.Admin.Models.ManageUserModels mu = new ManageBanking.Areas.Admin.Models.ManageUserModels();
                if (mu.Resolve(idrequest, lockuser, idtype, idadmin, idusername) == true)
                    Session["Res"] = "true";
                else
                    Session["Res"] = "Error,Please Try Again Later";

                return RedirectToAction("RequestAdmin", "ManageAdmin");
            }
            else
                return RedirectToAction("RequestAdmin", "ManageAdmin");
        }

        public JsonResult CheckName(FormCollection form)

        {
            System.Threading.Thread.Sleep(1000);

            string name = form["username"];
            if (name == "")
                return Json(false);
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Usernames.Where(a1 => a1.Username1.Equals(name)).Count();

 

            if (a!=0)

                return Json(false);

            return Json(true);

        }
        public ActionResult ChangeInforAdmin()
        {
            if (Session["Admin"] != null)
            {
                ManageBanking.Areas.Admin.Models.Username us = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                Session.Remove("UserAdmin");
                if (us.IdTypeUser == 1)
                {
                    ViewData["menuadmin"] = "ChangeInformationAd";
                    ViewData["User"] = Session["Admin"];
                    ViewData["Result"] = Session["Result"];
                    Session.Remove("Result");
                    return View();
                }
                else
                    return RedirectToAction("LoginAdmin", "LoginAdmin");
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");

        }



        public ActionResult CheckChangeInfo()
        { 
         if (Session["Admin"] != null)
            {
                ManageBanking.Areas.Admin.Models.Username us = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                if (us.IdTypeUser == 1)
                {
                    ViewData["menuadmin"] = "ChangeInformation";
                    ViewData["menuleft_bottom"] = "Change-Information";
                    ViewData["CheckII"] = Session["CheckII"];
                    ViewData["user1"] = Session["user1"];
                    Session.Remove("CheckII");
                    Session.Remove("user1");
                    Session.Remove("User");
                    return View();
                }

                return RedirectToAction("LoginAdmin", "LoginAdmin");
            }
            else
             return RedirectToAction("LoginAdmin", "LoginAdmin");
        
        }
        public ActionResult ChangeInfor()
        {
            if (Session["Admin"] != null && Session["UserAdmin"] != null)
            {
                ManageBanking.Areas.Admin.Models.Username us = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                Session.Remove("CheckII");
                if (us.IdTypeUser == 1)
                {
                    ViewData["menuadmin"] = "ChangeInformation";
                    ViewData["User"] = Session["UserAdmin"];
                    ViewData["Result"] = Session["Result"];
                    Session.Remove("Result");
                    return View();
                }
                else
                    return RedirectToAction("LoginAdmin", "LoginAdmin");
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");
            
        }
        public ActionResult ResolveInformation(string username, string name, string sex, DateTime birth, string address, string Email, string PlaceBirth, string job, string identificard, string SecPass, int idadmin, int iduser)
        {
            if (Session["Admin"] != null && username != null && name != null && sex != null && birth != null && address != null && address != null && Email != null && PlaceBirth != null && job != null && identificard != null && idadmin != 0 && iduser != 0)
            {
                ManageBanking.Areas.Admin.Models.Username us1 = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                if (us1.IdTypeUser == 1)
                {
                    Username us = (Username)Session["UserAdmin"];
                    if (mu.CheckUSer(username.Trim()) == true && us.Username1 != username.Trim())
                    {
                        Session["Result"] = "Username is already used";
                        return RedirectToAction("ChangeInfor");
                    }
                    else if (mu.AnswerIdentity(identificard) == true && us.IdentityCard != identificard.Trim())
                    {
                        Session["Result"] = "Identification card is already exist";
                        return RedirectToAction("ChangeInfor");

                    }
                    else if (mu.Answer_Email(Email) == true && us.Email != Email.Trim())
                    {
                        Session["Result"] = "Email is already exist";
                        return RedirectToAction("ChangeInfor");

                    }


                    else
                    {
                        if (mu.ChangeInformation(username, name, sex, birth, address, Email, PlaceBirth, job, identificard, SecPass, idadmin, iduser) == true)
                        {
                            Session["CheckII"] = "true";
                            Session.Remove("UserAdmin");
                            return RedirectToAction("CheckChangeInfo");
                        }
                        else
                        {
                            Session["Result"] = "Error,Try Again";

                        }

                    }
                    return RedirectToAction("ChangeInfor");
                }
                else
                    return RedirectToAction("LoginAdmin", "LoginAdmin");

            }
            else
                return RedirectToAction("Home", "Login");
        }
        public ActionResult ResolveInformationAdmin(string username, string name, string sex, DateTime birth, string address, string Email, string PlaceBirth, string job, string identificard, string SecPass, int idadmin, int iduser)
        {
            if (Session["Admin"] != null && username != null && name != null && sex != null && birth != null && address != null && address != null && Email != null && PlaceBirth != null && job != null && identificard != null && idadmin != 0 && iduser != 0)
            {
                ManageBanking.Areas.Admin.Models.Username us1 = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                if (us1.IdTypeUser == 1)
                {
                    ManageBanking.Areas.Admin.Models.Username us = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                    if (mu.CheckUSer(username.Trim()) == true && us.Username1 != username.Trim())
                    {
                        Session["Result"] = "Username is already used";
                        return RedirectToAction("ChangeInfor");
                    }
                    else if (mu.AnswerIdentity(identificard) == true && us.IdentityCard != identificard.Trim())
                    {
                        Session["Result"] = "Identification card is already exist";
                        return RedirectToAction("ChangeInfor");

                    }
                    else if (mu.Answer_Email(Email) == true && us.Email != Email.Trim())
                    {
                        Session["Result"] = "Email is already exist";
                        return RedirectToAction("ChangeInfor");

                    }


                    else
                    {
                        if (mu.ChangeInformation(username, name, sex, birth, address, Email, PlaceBirth, job, identificard, SecPass, idadmin, iduser) == true)
                        {
                            Session["Result"] = "true";
                            Session.Remove("User");
                            return RedirectToAction("ChangeInforAdmin");
                        }
                        else
                        {
                            Session["Result"] = "Error,Try Again";

                        }

                    }
                    return RedirectToAction("ChangeInfor");
                }
                else
                    return RedirectToAction("LoginAdmin", "LoginAdmin");

            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");
        }
        public ActionResult ResolveChangeInfo(string username, string idcard)
        {
            if (Session["Admin"] != null && username != null && idcard != null)
            {
                ManageBanking.Areas.Admin.Models.Username us = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                if (us.IdTypeUser == 1)
                {
                    RequestAdminModels ram = new RequestAdminModels();
                    LoginModels lg = new LoginModels();
                    if (mu.CheckUSer(username) == true)
                    {
                        Session["UserAdmin"] = lg.Login_Acc(username);


                    }

                    Username ac = (Username)Session["UserAdmin"];

                    if (mu.CheckUSer(username) == false)
                    {
                        Session["CheckII"] = "Username not exist,Try Again ";
                        Session["user1"] = username;

                        return RedirectToAction("CheckChangeInfo");

                    }
                    else if (mu.AnswerIdentity1(ac.Username1, idcard) == false)
                    {

                        Session["CheckII"] = "Wrong Identify Card,Try Again";



                        return RedirectToAction("CheckChangeInfo");

                    }
                    else
                    {
                        Session["CheckII"] = "true";
                        return RedirectToAction("ChangeInfor");

                    }
                }
                else
                    return RedirectToAction("LoginAdmin", "LoginAdmin");

            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");




        }


        public JsonResult CheckNameUser(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);

            string name = form["username"];
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            Username us = (Username)Session["Login"];
            int a = olbk.Usernames.Where(a1 => a1.Username1.Equals(name) && a1.IdTypeUser==2 && a1.Username1!= us.Username1).Count();



            if (a != 0)

                return Json(false);

            return Json(true);

        }

        public JsonResult CheckNameAdmin(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);

            string name = form["username"];
            ManageBanking.Areas.Admin.Models.Username admu = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];

            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Usernames.Where(a1 => a1.Username1.Equals(name) && a1.IdTypeUser != 2 && a1.Username1!= admu.Username1).Count();



            if (a != 0)

                return Json(false);

            return Json(true);

        }
        public JsonResult CheckNameAdminUpdate(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            Username us = new Username();
            string name = form["username"];
            ManageBanking.Areas.Admin.Models.Username usad = new ManageBanking.Areas.Admin.Models.Username(); 
            if (Session["UserAdmin"] != null)
            {
                us = (Username)Session["UserAdmin"];
                if (us.Username1.Equals(name))
                return Json(true);
            }
            else if (Session["Admin"] != null)
            {
                usad = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                 if(usad.Username1.Equals(name))
                return Json(true);
            

            }
            
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Usernames.Where(a1 => a1.Username1.Equals(name)).Count();
            
            
             if (a != 0)

                return Json(false);

            return Json(true);

        }


        public JsonResult CheckNameUpdate(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            Username us = new Username();

            if (Session["User"] != null)
            {
                us = (Username)Session["User"];
            }
            else if (Session["Login"] != null)
            {
                us = (Username)Session["Login"];


            }
            string name = form["username"];
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Usernames.Where(a1 => a1.Username1.Equals(name)).Count();

            if(us.Username1.Equals(name))
                return Json(true);
            if (a != 0)

                return Json(false);

            return Json(true);

        }
        public JsonResult CheckOldPassword(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            Username us = (Username)Session["Login"];
            string name = form["oldpassword"];
            ManageBanking.Models.LoginModels lg = new ManageBanking.Models.LoginModels();
           
            

            if (lg.DecryptString(us.Password, us.SecondaryPass)
            .Equals(name))
                return Json(true);
           
            return Json(false);

        }
        public JsonResult CheckOldPasswordAdmin(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            ManageBanking.Areas.Admin.Models.Username us = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
            string name = form["oldpassword"];
            ManageBanking.Models.LoginModels lg = new ManageBanking.Models.LoginModels();



            if (lg.DecryptString(us.Password, us.SecondaryPass)
            .Equals(name))
                return Json(true);

            return Json(false);

        }
        public ActionResult ChangeSecPass()
        {
            ViewData["menuadmin"] = "ChangeSecPass";
            ViewData["Check"] = Session["Check"];
            Session.Remove("Check");
            return View();
        }
        public ActionResult ChangePass()
        {
            if (Session["Admin"] != null)
            {
                ViewData["menuleft_bottom"] = "Change-Password";
                ViewData["menuleft_top"] = "ChangePassword";
                ViewData["Check"] = Session["Check"];
                Session.Remove("Check");

                return View();
            }
            else
                return RedirectToAction("LoginAdmin", "LoginAdmin");

            
        }
        public ActionResult ResolveChangePassword(string idaccount, string newpassword, string oldpassword)
        {
            if (Session["Admin"] != null && idaccount != null && oldpassword != null)
            {

                try
                {
                   
                    mu.ChangePassword(idaccount, newpassword);
                    Session["Check"] = "true";
                    ManageBanking.Areas.Admin.Models.ManageUserModels mu1 = new ManageBanking.Areas.Admin.Models.ManageUserModels();
                    ManageBanking.Areas.Admin.Models.Username us1 = mu1.TakeUserAdmin(Convert.ToInt32(idaccount));
                    Session["Admin"] = us1;

                }
                catch
                {
                    Session["Check"] = "Update Fail, Try Again";

                }
                return RedirectToAction("ChangePass");



            }
            else
                return RedirectToAction("Home", "Login");
        }
        public JsonResult CheckOldSecAdminPassword(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            ManageBanking.Areas.Admin.Models.Username us = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
            string name = form["oldpassword"];
            ManageUserModels mu = new ManageUserModels();
            ManageBanking.Models.LoginModels lg = new ManageBanking.Models.LoginModels();


            if (mu.getMD5(name).Equals(us.SecondaryPass))

                return Json(true);

            return Json(false);

        }
        public ActionResult ResolveChangeSecPassword(string newpassword)
        {
            if (Session["Admin"] != null && newpassword != null)
            {
                ManageBanking.Areas.Admin.Models.Username us = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                if (mu.ChangeSecPassAdmin(newpassword, us) == true)
                {
                    Session["Check"] = "true";
                    ManageBanking.Areas.Admin.Models.ManageUserModels mu1 = new ManageBanking.Areas.Admin.Models.ManageUserModels();
                    ManageBanking.Areas.Admin.Models.Username us2 = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"]; 
                    ManageBanking.Areas.Admin.Models.Username us1 = mu1.TakeUserAdmin(Convert.ToInt32(us2.IdUsername));
                    Session["Admin"] = us1;
                    
                }
                else
                    Session["Check"] = "Update Fail, Try Again";


                return RedirectToAction("ChangeSecPass");
            }
            else
                return RedirectToAction("Home", "Login");
        }

        public JsonResult CheckOldSecPassword(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            Username us = (Username)Session["Login"];
            string name = form["oldpassword"];
            if (name == "")
                return Json(false);
            ManageUserModels mu = new ManageUserModels();
            ManageBanking.Models.LoginModels lg = new ManageBanking.Models.LoginModels();
           

                if(mu.getMD5(name).Equals(us.SecondaryPass))

                return Json(true);

            return Json(false);

        }

        public JsonResult CheckIdentifyUpdate(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            Username us = new Username();

            if (Session["User"] != null)
            {
                us = (Username)Session["User"];
            }
            else if (Session["Login"] != null)
            {
                us = (Username)Session["Login"];


            }
            string name = form["identificard"];
            if (name == "")
                return Json(false);
            else if (name.Length < 9 || name.Length > 20)
                return Json(false);
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Usernames.Where(a1 => a1.IdentityCard.Equals(name)).Count();


            if(us.IdentityCard.Equals(name))
                return Json(true);
            if (a != 0)

                return Json(false);

            return Json(true);

        }
        public JsonResult CheckIdentifyAdminUpdate(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            Username us = new Username();
            string name = form["identificard"];
            if(name=="")
                return Json(false);
            else if (name.Length < 9 || name.Length > 20)
                return Json(false);
            ManageBanking.Areas.Admin.Models.Username usad = new ManageBanking.Areas.Admin.Models.Username(); 
            if (Session["UserAdmin"] != null)
            {
                us = (Username)Session["UserAdmin"];
                
                 if (us.IdentityCard.Equals(name))
                return Json(true);
            }
            else if (Session["Admin"] != null)
            {

                usad = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                if (usad.IdentityCard.Equals(name))
                return Json(true);

            }
           
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Usernames.Where(a1 => a1.IdentityCard.Equals(name)).Count();
           

           
            
            if (a != 0)

                return Json(false);

            return Json(true);

        }

        public JsonResult CheckIdentify(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);

            string name = form["identificard"];
            if (name == "")
                return Json(false);
            else if (name.Length<9 || name.Length>20)
                    return Json(false);

            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Usernames.Where(a1 => a1.IdentityCard.Equals(name)).Count();



            if (a != 0)

                return Json(false);

            return Json(true);

        }
        public JsonResult CheckBirthday(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);

            string name = form["birth"];
            if (name == "")
                return Json(false);
            DateTime now = DateTime.Now;
            DateTime select = Convert.ToDateTime(name);
            if(now.AddYears(-18)>select)
                return Json(true);



           

            return Json(false);

        }

        public JsonResult CheckCaptcha(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);
            string cap = Session["captch"].ToString();
            string name = form["captcha"];
            if (name == "")
                return Json(false);
            
            if(cap.Trim()!=name)
                return Json(false);
            


            
            return Json(true);

        }


        public JsonResult CheckEmaiAdminlUpdate(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);

            string name = form["Email"];
            Username us = new Username();
            if (name == "")
                return Json(false);
            ManageBanking.Areas.Admin.Models.Username usad = new ManageBanking.Areas.Admin.Models.Username();
            if (Session["UserAdmin"] != null)
            {

                us = (Username)Session["UserAdmin"];
                if (us.Email.Equals(name))
                return Json(true);

            }
            else if (Session["Admin"] != null)
            {
                usad = (ManageBanking.Areas.Admin.Models.Username)Session["Admin"];
                if (usad.Email.Equals(name))
                return Json(true);
            }

            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Usernames.Where(a1 => a1.Email.Equals(name)).Count();


            
            

           if (a != 0)

                return Json(false);

            return Json(true);

        }




        public JsonResult CheckEmailUpdate(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);

            string name = form["Email"];
            if (name == "")
                return Json(false);
            Username us = new Username();

            if (Session["User"] != null)
            {
                us = (Username)Session["User"];
            }
            else if (Session["Login"] != null)
            {
                us = (Username)Session["Login"];
            
            
            }

            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Usernames.Where(a1 => a1.Email.Equals(name)).Count();
            if(us.Email.Equals(name))
                return Json(true);


            if (a != 0)

                return Json(false);

            return Json(true);

        }
        public JsonResult CheckEmail(FormCollection form)
        {
            System.Threading.Thread.Sleep(1000);

            string name = form["Email"];
            if (name == "")
                return Json(false);
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();
            int a = olbk.Usernames.Where(a1 => a1.Email.Equals(name)).Count();



            if (a != 0)

                return Json(false);

            return Json(true);

        }
       
        public JsonResult CompleteUser(string term)
        {
            OnlineBankingDataContext olbk = new OnlineBankingDataContext();

            var customers = olbk.Usernames.Where(a =>a.IdTypeUser == 2 && a.Username1.StartsWith(term)).Select(a => a.Username1).Skip(0).Take(5).ToArray();

           
            return Json(customers, JsonRequestBehavior.AllowGet);
        }

    }
}
